﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Cartoon.Config;
using Cartoon.Model;
using System.ComponentModel;
using Cartoon.Common;

namespace CartoonViewer
{
    public class CartoonListPanel : ListPanel, ICartoonList
    {

        public CartoonListPanel()
            : base(9)
        {
            this.BGWorkerLoadCartoon = new BackgroundWorker();
            this.BGWorkerLoadCartoon.DoWork += new DoWorkEventHandler(BGWorkerLoadCartoon_DoWork);
            this.BGWorkerLoadCartoon.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BGWorkerLoadCartoon_RunWorkerCompleted);

            this.BGWorkerLoadCartoon.WorkerSupportsCancellation = true;
        }

        private BackgroundWorker BGWorkerLoadCartoon;

        private List<UC_Cartoon> _cartoonList;
        protected List<UC_Cartoon> CartoonList
        {
            get
            {
                if (this._cartoonList == null)
                {
                    this._cartoonList = new List<UC_Cartoon>();
                }
                return this._cartoonList;
            }
            set
            {
                this._cartoonList = value;
            }
        }

        private void BGWorkerLoadCartoon_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        private void BGWorkerLoadCartoon_DoWork(object sender, DoWorkEventArgs e)
        {
            Log.log("加载漫画列表...");

            this.Clear();
            this.ComicList = this.Config.LoadCartoonList();
            foreach (Comic comic in this.ComicList)
            { 
                this.Dispatcher.Invoke(new Action(() =>
                {
                    UC_Cartoon uc_ct = new UC_Cartoon(comic);
                    uc_ct.Selected += new CartoonSelectedDelegate(uc_ct_Selected);
                    this.AddCartoon(uc_ct);
                })); 
            }
        }

        private UC_Cartoon CurrentCartoon;
        private void uc_ct_Selected(object sender, Comic comic)
        {
            if (this.ChapterList.IsLoadingChapter) return;

            UC_Cartoon uc_ct = sender as UC_Cartoon;

            if (uc_ct == null || this.CurrentCartoon == uc_ct) return;

            if (this.CurrentCartoon != null)
            {
                this.CurrentCartoon.IsSelected = false;
            }
            uc_ct.IsSelected = true;
            this.CurrentCartoon = uc_ct;

            this.ChapterList.CurrentComic = comic;
            this.ChapterList.LoadChapterList();
        }

        protected void AddCartoon(UC_Cartoon uc_ct)
        {
            if (uc_ct == null || this.CartoonList.Contains(uc_ct) == true)
            {
                return;
            }

            this.CartoonList.Add(uc_ct);
            this.Children.Add(uc_ct);

            uc_ct.Margin = new Thickness(this.Margin_Left, this.Margin_Top, 0, 0);
            uc_ct.SetValue(Grid.RowProperty, this.RowNum);
            uc_ct.SetValue(Grid.ColumnProperty, this.ColumnNum);

            if (this.RowDefinitions.Count < this.RowNum + 1)
            {
                this.RowDefinitions.Add(new RowDefinition());
            }
        }

        #region ICartoonList 成员

        public IChapterList ChapterList
        {
            get;
            set;
        }

        public List<Comic> ComicList
        {
            get;
            set;
        }

        public ConfigForCartoon Config
        {
            get;
            set;
        }

        public List<UC_Cartoon> GetCartoonList()
        {
            return this.CartoonList;
        }


        public int CartoonNum { get { return this.CartoonList.Count; } }

        public void LoadComics()
        {
            this.BGWorkerLoadCartoon.RunWorkerAsync();
        }

        #endregion

        #region IPanelList 成员

        public override void Clear()
        { 
            this.Dispatcher.Invoke
            (
                new Action
                (
                    () =>
                    { 
                        base.Clear(); 
                    }
                )
            ); 

            this.CartoonList.Clear();
        }


        #endregion
    }
}
